package com.example.pungolo.journal;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;

public class SettingsActivity extends Activity {
  public final static String[] fontPaths = { null, "fonts/Athena_Unicode.ttf", "fonts/CAC_Champagne.ttf", "fonts/Futura-Book.ttf", "fonts/Gabrielle.ttf", "fonts/Jacoba_Bold.ttf" };
  private final static String[] fontNames = { "Default", "Athena", "Champagne", "Futura-Book", "Gabrielle", "Jacoba" };
  private final static Integer[] fontSizes = { 6,7,8,9,10,11,12,14,18,24,30,36,48,60,72,96 };

  public final static String GRACE_TIME = "tempoSalvato";
  public final static String FONT_TYPE = "fontSalvato";
  public final static String FONT_SIZE = "dimSalvata";

  public final static int DEFAULT_FONT_SIZE = 24;
  public final static int DEFAULT_GRACE_TIME = 30;
  public final static int MINIMUM_GRACE_TIME = 5;
  
  private SeekBar graceTimeSeekBar;
  private TextView graceTimeLabel;
  private Spinner fontTypeSpinner;
  private Spinner fontSizeSpinner;
  private SharedPreferences preferences;
  
  private Typeface[] typefaces;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    
    this.preferences = getSharedPreferences("settings", 0);
    this.graceTimeSeekBar = (SeekBar) findViewById(R.id.barraTempo);
    this.graceTimeLabel = (TextView) findViewById(R.id.testoTempo);
    this.fontTypeSpinner = (Spinner) findViewById(R.id.listaFont);
    this.fontSizeSpinner = (Spinner) findViewById(R.id.dimFont);
    
    int graceTime = preferences.getInt(GRACE_TIME, DEFAULT_GRACE_TIME);
    String fontType = preferences.getString(FONT_TYPE, null);
    int fontSize = preferences.getInt(FONT_SIZE, DEFAULT_FONT_SIZE);

    ArrayAdapter<String> fontTypesAdapter = new FontTypeSpinnerAdapter(this, android.R.layout.simple_spinner_item);
    fontTypeSpinner.setAdapter(fontTypesAdapter);
    ArrayAdapter<Integer> fontSizesAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, fontSizes);
    fontSizeSpinner.setAdapter(fontSizesAdapter);
    
    graceTimeSeekBar.setProgress(graceTime - MINIMUM_GRACE_TIME);
    graceTimeLabel.setText(String.valueOf(graceTime) + " minuti");
    fontTypeSpinner.setSelection(U.indexOf(fontPaths, fontType));
    fontSizeSpinner.setSelection(U.indexOf(fontSizes, fontSize));

    fontSizeSpinner.setOnItemSelectedListener(new FontSizeSpinnerItemSelectedListener());
    fontTypeSpinner.setOnItemSelectedListener(new FontTypeSpinnerItemSelectedListener());
    graceTimeSeekBar.setOnSeekBarChangeListener(new GraceTimeSeekBarChangedListener());
    
    typefaces = new Typeface[fontPaths.length];
    for (int i = 0; i < typefaces.length; i++)
      typefaces[i] = fontPaths[i] == null ? null : Typeface.createFromAsset(getAssets(), fontPaths[i]);
  }
  
  public static void setCustomFont(Context c, TextView v) {
    SharedPreferences p = c.getSharedPreferences("settings", 0);
    String fontPath = p.getString(SettingsActivity.FONT_TYPE, null);
    int fontSize = p.getInt(FONT_SIZE, DEFAULT_FONT_SIZE);

    v.setTextSize(fontSize);
    if (fontPath != null)
      v.setTypeface(Typeface.createFromAsset(c.getAssets(), fontPath));
  }
  
  private class GraceTimeSeekBarChangedListener implements OnSeekBarChangeListener {
    public void onStopTrackingTouch(SeekBar seekBar) {
      int tempoSalvato = MINIMUM_GRACE_TIME + graceTimeSeekBar.getProgress();
      Editor e = preferences.edit();
      e.putInt(GRACE_TIME, tempoSalvato);
      e.commit();
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
      graceTimeLabel.setText(R.string._minutes);
      graceTimeLabel.setText(String.valueOf(MINIMUM_GRACE_TIME + progress) + graceTimeLabel.getText());
    }
  }
  
  private class FontSizeSpinnerItemSelectedListener implements OnItemSelectedListener {
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
      int dimSalvata = fontSizes[position];
      Editor e = preferences.edit();

      e.putInt(FONT_SIZE, dimSalvata);
      e.commit();
    }
    
    public void onNothingSelected(AdapterView<?> parent) {
    }
  }
  
  private class FontTypeSpinnerItemSelectedListener implements OnItemSelectedListener {
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
      String fontSalvato = fontPaths[position];
      Editor e = preferences.edit();

      e.putString(FONT_TYPE, fontSalvato);
      e.commit();
    }
    
    public void onNothingSelected(AdapterView<?> parent) {
    }
  }
  
  private class FontTypeSpinnerAdapter extends ArrayAdapter<String> {
    public FontTypeSpinnerAdapter(Context context, int textViewResourceId) {
      super(context, textViewResourceId, fontNames);
    }
    
    public TextView getView(int position, View convertView, ViewGroup parent) {
      TextView result = (TextView)super.getView(position, convertView, parent);
      if (typefaces[position] != null)
        result.setTypeface(typefaces[position]);
      return result;
    }
  }
}
